package cn.jbit.easybuy.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.jbit.easybuy.dao.BaseDao;
import cn.jbit.easybuy.dao.ProductCategoryDao;
import cn.jbit.easybuy.entity.ProductCategory;

public class ProductCategoryDaoImpl extends BaseDao implements ProductCategoryDao {

	@Override
	public List<ProductCategory> findParentCategory() {
		List<ProductCategory> list = new ArrayList<ProductCategory>();
		Connection conn = getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			String sql = "select * from easybuy_product_category where epc_id = epc_parent_id";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()){
				ProductCategory pc = new ProductCategory();
				pc.setId(rs.getLong(1));
				pc.setName(rs.getString(2));
				pc.setParentId(rs.getLong(3));
				list.add(pc);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeAll(conn, ps, rs);
		}
		return list;
	}

	@Override
	public List<ProductCategory> findChildCategory(long pid) {
		List<ProductCategory> list = new ArrayList<ProductCategory>();
		Connection conn = getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			String sql = "select * from easybuy_product_category where epc_parent_id = ?";
			ps = conn.prepareStatement(sql);
			ps.setLong(1, pid);
			rs = ps.executeQuery();
			while(rs.next()){
				ProductCategory pc = new ProductCategory();
				pc.setId(rs.getLong(1));
				pc.setName(rs.getString(2));
				pc.setParentId(rs.getLong(3));
				list.add(pc);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeAll(conn, ps, rs);
		}
		return list;
	}

}
